#include "interfazSistema.h"
#include "interfazUsuario/cimahis.h"
#include "interfazUsuario/areaImagen.h"
#include "imagen.h"

/*IplImage* obtenerImagenActual();
{
cimahis *area = new cimahis;
return area->mdiHijoActivo()->obtenerImagen().obtenerImagenCv();
}
*/
QImage IplImage2QImage(const IplImage *iplImage)
{
    int height = iplImage->height;
    int width = iplImage->width;
    if(iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)
        {
        const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
        QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
        return img.rgbSwapped();
        }
    else
        {
        //qWarning() << "Image cannot be converted.";
        return QImage();
        }
}

IplImage* qImage2IplImage(const QImage& qImage)
{
    int width = qImage.width();
    int height = qImage.height();
    // Creates a iplImage with 3 channels
    IplImage *img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
    char * imgBuffer = img->imageData;
    //Remove alpha channel
    int jump = (qImage.hasAlphaChannel()) ? 4 : 3;
    for (int y=0;y<img->height;y++)
        {
        QByteArray a((const char*)qImage.scanLine(y), qImage.bytesPerLine());
        for (int i=0; i<a.size(); i+=jump)
            {
            //Swap from RGB to BGR
            imgBuffer[2] = a[i];
            imgBuffer[1] = a[i+1];
            imgBuffer[0] = a[i+2];
            imgBuffer+=3;
            }
        }
return img;
}

IplImage* convertirQImage_IplImage(QImage imagen)
{
    IplImage* cvImagen = cvCreateImageHeader(cvSize(imagen.width(), imagen.height()), IPL_DEPTH_8U, 4);
    cvImagen->imageData = (char*)imagen.bits();
    return cvImagen;
}

IplImage *QImageToIplImage(const QImage * qImage)
{
    int width = qImage->width();
    int height = qImage->height();

    CvSize Size;
    Size.height = height;
    Size.width = width;

    IplImage *charIplImageBuffer = cvCreateImage(Size, IPL_DEPTH_8U, 1);
    char *charTemp = (char *) charIplImageBuffer->imageData;

    for (int y = 0; y < height; ++y)
        {
        for (int x = 0; x < width; ++x)
            {
            int index = y * width + x;
            charTemp[index] = (char) qGray(qImage->pixel(x, y));
            }
        }

    return charIplImageBuffer;
}


///////////////////////////////////////////////////////

IplImage* QImage2IplImage(QImage *qimg)
{

    IplImage *imgHeader = cvCreateImageHeader( cvSize(qimg->width(), qimg->width()), IPL_DEPTH_8U, 4);
    imgHeader->imageData = (char*) qimg->bits();

    uchar* newdata = (uchar*) malloc(sizeof(uchar) * qimg->byteCount());
    memcpy(newdata, qimg->bits(), qimg->byteCount());
    imgHeader->imageData = (char*) newdata;
    //cvClo
    return imgHeader;
    }

QImage*  IplImageToQImage(IplImage *iplImg)
{
    int h = iplImg->height;
    int w = iplImg->width;
    int channels = iplImg->nChannels;
    QImage *qimg = new QImage(w, h, QImage::Format_ARGB32);
    char *data = iplImg->imageData;

    for (int y = 0; y < h; y++, data += iplImg->widthStep)
        {
        for (int x = 0; x < w; x++)
            {
            char r, g, b, a = 0;
            if (channels == 1)
                {
                r = data[x * channels];
                g = data[x * channels];
                b = data[x * channels];
                }
            else if (channels == 3 || channels == 4)
                {
                r = data[x * channels + 2];
                g = data[x * channels + 1];
                b = data[x * channels];
                }

            if (channels == 4)
                {
                a = data[x * channels + 3];
                qimg->setPixel(x, y, qRgba(r, g, b, a));
                }
            else
                {
                qimg->setPixel(x, y, qRgb(r, g, b));
                }
            }
    }
    return qimg;

}
